using System;
using System.IO;
using System.Xml;

using Icodeon;
using Icodeon.Xml;
using Icodeon.Util;

using NUnit.Framework;


namespace Icodeon.Scorm2CC
{
    [TestFixture]
    public class Scorm_2004_LSAL_Template_10_Test : BaseTest
    {
        IOUtils ioUtils;
        ScormUtils scormUtils;
        XmlUtils xmlUtils;

        [TestFixtureSetUp]
        public void Init()
        {
            this.SetUp();

            ioUtils = new IOUtils();
            scormUtils = new ScormUtils();
            xmlUtils = new XmlUtils();

            DirectoryInfo scormPackages =
                ioUtils.CombineParentAndChildDirectoryNames(this.rootDirectory, "packages");
            DirectoryInfo sourceDirectory = ioUtils.CombineParentAndChildDirectoryNames(scormPackages, "scorm_2004_lsal_10");

            this.scormPackageDirectory = ioUtils.CombineParentAndChildDirectoryNames(this.temp, "scorm/" + Guid.NewGuid().ToString());
            ioUtils.MakeDirectoriesInPath(this.scormPackageDirectory);

            Directory.Delete(this.scormPackageDirectory.FullName, true);

            System.Console.WriteLine("Copying to: " + this.scormPackageDirectory.FullName);
            xDirectory.Copy(sourceDirectory, this.scormPackageDirectory, true);

            DirectoryInfo xslDirectory = ioUtils.CombineParentAndChildDirectoryNames(this.rootDirectory, "xsl");
            this.xslFile = ioUtils.CombineDirectoryNameAndFileName(xslDirectory, "scorm2004cc.xslt");

            FileInfo imsManifestScorm = ioUtils.CombineDirectoryNameAndFileName(this.scormPackageDirectory, "imsmanifest.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(imsManifestScorm.FullName);

            this.manifest = doc.DocumentElement;
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
        }

        [Test]
        public void TestLSALTemplate10()
        {
            try
            {
                // Check the SCORM version

                ScormVersion scormVersion = scormUtils.GetScormVersion(this.manifest);
                Assert.AreEqual(ScormVersion.scorm2004_2ed, scormVersion);

                XmlElement resources = this.GetResources(this.manifest);
                XmlElement[] resourceElements = this.GetResourceElements(resources);
                for (int i = 0; i < resourceElements.Length; i++)
                {
                    ScormType scormType = scormUtils.GetScormType(resourceElements[i], scormVersion);
                    Assert.AreEqual(ScormType.sco, scormType);
                }


                IPackageConverter converter = new PackageConverterImpl();
                converter.WrapAndCopyResourceFiles(this.scormPackageDirectory, this.commonCartridgeDirectory, this.scorm2ccDirectory, null);
                converter.TransformImsManifest(this.scormPackageDirectory, this.commonCartridgeDirectory, this.xslFile, this.xslFile, null);

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        private XmlElement GetResources(XmlElement manifest)
        {
            return this.xmlUtils.GetNamedChildElement(manifest, "resources");
        }

        private XmlElement[] GetResourceElements(XmlElement resources)
        {
            return this.xmlUtils.GetNamedChildElements(resources, "resource");
        }
    }
}
